package com.zimmur.quartz;


import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.zimmur.common.ConstData;
import com.zimmur.service.ISolrService;
import com.zimmur.service.RedisCacheUtil;
import com.zimmur.tools.DateUtils;

@Component
public class ProcessSolrUpdate {
	private Logger log = LoggerFactory.getLogger(ProcessSolrUpdate.class);
	@Resource
	private ISolrService solrService;
	@Resource
	private RedisCacheUtil<Integer> stringRedisDao;
	
	private static final String solr_time_goods="popolook_solr_updateTime_goods";
	private static final String solr_time_designer="popolook_solr_updateTime_designer";
	private static final String solr_time_topic="popolook_solr_updateTime_topic";
	
	private static final String solr_goodsIds=ConstData.solr_goodsIds;
	private static final String solr_topicIds=ConstData.solr_topicIds;
	private static final String solr_designerIds=ConstData.solr_designerIds;
	/**
     * 每隔30分钟执行一次
     * @Scheduled(fixedRate = 30*60 * 1000) 
     */
	@Scheduled(fixedRate=30*60*1000)
	public void handlerGoods(){
		log.info("handlerGoods task begin");
		String goodsIds=null;
		boolean goodsIdExist=false;
		boolean timeExist=false;
		Integer time=0;
		Integer nowTime=DateUtils.getNowUnixTime();
		try {
			goodsIdExist=stringRedisDao.exists(solr_goodsIds);
			timeExist=stringRedisDao.exists(solr_time_goods);
			
			if(timeExist){
				time=Integer.parseInt(stringRedisDao.getCacheString(solr_time_goods));
			}else{
				time=nowTime;
			}
			
			if(goodsIdExist){
				goodsIds=stringRedisDao.getCacheString(solr_goodsIds);
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		if(goodsIdExist && !StringUtils.isEmpty(goodsIds)){
			if(goodsIds.endsWith(",")){
				goodsIds=goodsIds.substring(0, goodsIds.length()-1);
			}
			solrService.addGoodses(goodsIds);
		}
		try {
			stringRedisDao.setCacheString(solr_time_goods, String.valueOf(nowTime));
			stringRedisDao.setCacheString(solr_goodsIds, "");
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		log.info("handlerGoods task end");
	}
	/**
	 * 每隔30分钟执行一次
	 * @Scheduled(fixedRate = 30*60 * 1000) 
	 */
	@Scheduled(fixedRate=30*60*1000)
	public void handlerDesigner(){
		log.info("handlerDesigner task begin");
		String designerIds=null;
		boolean goodsIdExist=false;
		boolean timeExist=false;
		Integer time=0;
		Integer nowTime=DateUtils.getNowUnixTime();
		try {
			goodsIdExist=stringRedisDao.exists(solr_designerIds);
			timeExist=stringRedisDao.exists(solr_time_designer);
			
			if(timeExist){
				time=Integer.parseInt(stringRedisDao.getCacheString(solr_time_designer));
			}else{
				time=nowTime;
			}
			
			if(goodsIdExist){
				designerIds=stringRedisDao.getCacheString(solr_designerIds);
				
			}
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
		if(goodsIdExist && !StringUtils.isEmpty(designerIds)){
			if(designerIds.endsWith(",")){
				designerIds=designerIds.substring(0, designerIds.length()-1);
			}
			solrService.addDesigners(designerIds);
		}
		try {
			stringRedisDao.setCacheString(solr_time_designer, String.valueOf(nowTime));
			stringRedisDao.setCacheString(solr_designerIds, "");
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		log.info("handlerDesigner task end");
	}
	/**
	 * 每隔30分钟执行一次
	 * @Scheduled(fixedRate = 30*60 * 1000) 
	 */
	@Scheduled(fixedRate=30*60*1000)
	public void handlerTopic(){
		log.info("handlerTopic task begin");
		String topicIds=null;
		boolean goodsIdExist=false;
		boolean timeExist=false;
		Integer time=0;
		Integer nowTime=DateUtils.getNowUnixTime();
		try {
			goodsIdExist=stringRedisDao.exists(solr_topicIds);
			timeExist=stringRedisDao.exists(solr_time_topic);
			
			if(timeExist){
				time=Integer.parseInt(stringRedisDao.getCacheString(solr_time_topic));
			}else{
				time=nowTime;
			}
			
			if(goodsIdExist){
				topicIds=stringRedisDao.getCacheString(solr_topicIds);
			}
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
		if(goodsIdExist && !StringUtils.isEmpty(topicIds)){
			if(topicIds.endsWith(",")){
				topicIds=topicIds.substring(0, topicIds.length()-1);
			}
			solrService.addTopics(topicIds);
		}
		try {
			stringRedisDao.setCacheString(solr_time_topic, String.valueOf(nowTime));
			stringRedisDao.setCacheString(solr_topicIds, "");
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		log.info("handlerTopic task end");
	}
}
